package com.example;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.realm.SimpleAccountRealm;
import org.apache.shiro.subject.Subject;
import org.junit.Before;
import org.junit.Test;

/**
 * @ClassName QuickStartTest4_2
 * @Description TODO
 * @Author zhaoluowei
 * @Date 2019/6/30  22:36
 * @Version 1.0
 */

public class QuickStartTest4_2 {

    private SimpleAccountRealm accountRealm = new SimpleAccountRealm();

    private DefaultSecurityManager securityManager = new DefaultSecurityManager();


    @Before
    public void init() {
        //初始化数据源
        accountRealm.addAccount("zhangsan", "123");
        accountRealm.addAccount("lisi", "456");
        accountRealm.addAccount("wangwu", "789");
        accountRealm.addAccount("jack", "123");

        //构建环境
        securityManager.setRealm(accountRealm);

    }

    @Test
    public void test() {
        SecurityUtils.setSecurityManager(securityManager);

        //获取当前的主体
        Subject subject = SecurityUtils.getSubject();

        //构造需要登录用户信息 用户用的
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken("zhangsan","123");

        subject.login(usernamePasswordToken);
        System.out.println("认证结果 --> "+subject.isAuthenticated());


    }


}
